import {
  Module,
  Global,
  OnModuleInit,
  OnApplicationBootstrap,
  OnModuleDestroy,
  BeforeApplicationShutdown,
  OnApplicationShutdown,
  Inject,
  forwardRef,
} from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { LifeCycleService } from './life-cycle.service';
import { LifeCycleController } from './life-cycle.controller';
import { ArgumentContextModule } from '../argument-context/argument-context.module';

// @Global()
@Module({
  imports: [forwardRef(() => ArgumentContextModule)],
  controllers: [LifeCycleController],
  providers: [LifeCycleService],
  exports: [LifeCycleService],
})
export class LifeCycleModule
  implements
    OnModuleInit,
    OnApplicationBootstrap,
    OnModuleDestroy,
    BeforeApplicationShutdown,
    OnApplicationShutdown
{
  @Inject(ModuleRef)
  private moduleRef: ModuleRef;

  onModuleDestroy() {}

  beforeApplicationShutdown(signal: string) {}

  onModuleInit() {}

  onApplicationBootstrap() {}

  onApplicationShutdown() {
    // 获取当前模块 service 实例
    const lifeCycleService =
      this.moduleRef.get<LifeCycleService>(LifeCycleService);
    console.log('应用关闭后', lifeCycleService.test());
  }
}
